﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Model;
using System.Data;
using System.Text;
using DotNet.Model.MyEntity;
using NBear.Mapping;
using KingRoad.EPM.Web.Util;
//using KingRoad.EPM.InvestmeEntity;
using DotNet.Service;
using DotNet.Utilities;
using KingRoad.EPM.Web.ViewModels;


namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    /// <summary>
    /// 选项字典管理
    /// 功能描述：字典的删、查及选项的增、删、改、查等等
    /// 模块负责人：张雨
    /// 完成日期：2011.9.23
    /// </summary>
    public class ItemsController : Controller
    {
        //
        // GET: /Permission/Items/
        /// <summary>
        /// 选项字典管理首页加载
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 加载选项字典数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetItemsByPage(DotNet.Model.MyEntity.QueryBase query)
        {
            List<Base_Items> list = ServiceManager.Instance.ItemsService.GetDT();
            var jqGridData = WebUtility.TojqGridData<Base_Items>(list, query);
            return Json(jqGridData);
        }

        /// <summary>
        /// 添加选项字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>        
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Permission/Items/Create
        /// <summary>
        /// 字典的新增操作
        /// </summary>
        /// <param name="Items"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Creating(Base_Items Items)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(Items.TargetTable) || Items.TargetTable == "Items_")
                {
                    statuCode = "300";
                    statuMessage = "表名不可为空";
                }
                else
                {
                    Items.IsCreateTable = 1;
                    string id = ServiceManager.Instance.ItemsService.AddItems(Items.TargetTable, Items, out statuCode, out statuMessage);
                }

            }

            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode == "OK" ? "200" : "300",
                callbackType = "Create"
            });
        }

        //
        // GET: /Permission/Items/Edit/5
        /// <summary>
        /// 字典的编辑页加载
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            Base_Items items = ServiceManager.Instance.ItemsService.GetEntity(id.ToString());

            return View(items);
        }

        //
        // POST: /Permission/Items/Edit/5
        /// <summary>
        /// 字典的编辑操作
        /// </summary>
        /// <param name="Items"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Editing(Base_Items Items)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                ServiceManager.Instance.ItemsService.UpdateItems(Items, out statuCode, out statuMessage);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode == "OKUpdate" ? "200" : "300",
                callbackType = "Edit",
                data = Items
            });
        }

        //
        // GET: /Permission/Items/Delete/5
        /// <summary>
        /// 字典批量删除操作
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete()
        {
            string statuCode;
            string statuMessage;
            string ItemsArryID = Request.Params["ItemsArryID"];
            if (string.IsNullOrEmpty(ItemsArryID))
            {
                statuCode = "300";
                statuMessage = "参数出现异常!";
            }
            else
            {
                if (ItemsArryID.EndsWith(","))
                    ItemsArryID = ItemsArryID.Substring(0, ItemsArryID.Length - 1);
                ServiceManager.Instance.ItemsService.Delete(ItemsArryID, out statuCode, out statuMessage);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "delete",
            });
        }

        /// <summary>
        /// 选项细目的查看
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            ViewBag.ItemID = id;
            return View();
        }

        /// <summary>
        /// 获取细目信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDetails(string id, DotNet.Model.MyEntity.QueryBase query, int? nodeid, int? n_level)
        {

            Base_Items baseItem = ServiceManager.Instance.ItemsService.GetEntity(id);
            List<BaseItemDetailsEntity> list = null;
            if (baseItem.IsTree == 1)
            {
                //获取首层
                if (nodeid.HasValue)
                {
                    list = ServiceManager.Instance.ItemDetailsService.GetItemsByParentId(nodeid.Value, n_level.Value, baseItem.Code);
                }
                else
                    list = ServiceManager.Instance.ItemDetailsService.GetItemsByParentId(0, 0, baseItem.Code);

            }
            else
            {
                list = ServiceManager.Instance.ItemsService.GetTiemDetailsDT(baseItem.TargetTable);

            }
            JsonResult json = new JsonResult();

            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            var jqGridData = WebUtility.TojqGridData<BaseItemDetailsEntity>(list, query);
            json.Data = jqGridData;
            return json;



        }

        /// <summary>
        /// 添加选项细目
        /// </summary>
        /// <param name="id"></param>
        /// <param name="detailID">上级细目ID</param>
        /// <returns></returns>
        public ActionResult DetailsCreate(string id, string detailID)
        {
            ViewBag.ItemID = id;
            if (String.IsNullOrEmpty(detailID))
                ViewBag.ParentId = 0;
            else
                ViewBag.ParentId = detailID;
            return View();
        }

        /// <summary>
        /// 添加选项细目操作
        /// </summary>
        /// <param name="details"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DetailsCreating(ItemDetails details)
        {
            string statuCode;
            string statuMessage;
            string itemId = Request["ItemID"];
            string tableName = ServiceManager.Instance.ItemsService.GetEntity(itemId).TargetTable;

            ServiceManager.Instance.ItemsService.AddTiemDetails(details, tableName, out statuCode, out statuMessage);

            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode.ToLower() == "okadd" ? "200" : "300",
                callbackType = "delete",
            });
        }

        /// <summary>
        /// 选项细目的编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DetailsEdit(string id, string selectId)
        {
            ItemDetails Details = null;

            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(selectId))
            {
                ViewBag.ItemID = id;
                Details = ServiceManager.Instance.ItemsService.GetTiemDetailsEntity(ServiceManager.Instance.ItemsService.GetEntity(id).TargetTable, selectId);
                ViewBag.ParentId = Details.ParentId;
                ViewBag.Id = Details.Id;
            }            
            return View(Details);
        }

        /// <summary>
        /// 选项细目的编辑操作
        /// </summary>
        /// <param name="details"></param>
        /// <returns></returns>
        public ActionResult DetailsEditing(ItemDetails details, string ItemID)
        {
            string statuCode;
            string statuMessage;
            if (string.IsNullOrEmpty(details.Id.ToString()))
            {
                statuCode = "300";
                statuMessage = "数据结构出现异常，字典ID为空！";
            }
            else
            {
                ServiceManager.Instance.ItemsService.UpdateTiemDetails(ServiceManager.Instance.ItemsService.GetEntity(ItemID).TargetTable, details, out statuCode, out statuMessage);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode.ToLower() == "okupdate" ? "200" : "300",
                callbackType = "update",
            });
        }

        /// <summary>
        /// 批量删除的方法
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DetailsDelete(string ids)
        {
            string statuCode = "300";
            string statuMessage = "出现异常";
            string ItemsArryID = Request.Params["DetailsArryID"];
            string itemID = Request.Params["itemID"];
            if (string.IsNullOrEmpty(ItemsArryID) || string.IsNullOrEmpty(itemID))
            {
                statuCode = "300";
                statuMessage = "参数出现异常!";
            }
            else
            {
                string tablename = ServiceManager.Instance.ItemsService.GetEntity(itemID).TargetTable;
                if (!string.IsNullOrEmpty(tablename) && tablename != "Items_")
                {
                    if (ItemsArryID.EndsWith(","))
                        ItemsArryID = ItemsArryID.Substring(0, ItemsArryID.Length - 1);
                    ServiceManager.Instance.ItemsService.BatchDeleteTiemDetails(tablename, ItemsArryID, out statuCode, out statuMessage);
                }
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "delete",
            });
        }
    }
}
